package org.example.design.designmode.prototype.jsonclone;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: songyafei
 * @Package: com.des.design.designMode.prototype
 * @ClassName: user
 * @Description: 用户类
 * @Param
 * @Exception
 * @CreateDate: 2020/5/11 21:24
 * @UpdateRemark: The modified content
 * @Version: 1.0
 * @Return :
 **/

public class User implements Serializable {

    private Integer id;

    private String idCard;

    private String name;

    private Integer age;

    private Long ismale;


    /**
     * @Author: songyafei
     * @Package: com.des.design.designMode.prototype
     * @ClassName: user
     * @Description: 传统业务中对象的复制创建需要我们创建实例在一个个set进行赋值
     * @Param [u]
     * @Exception
     * @CreateDate: 2020/5/11 21:28
     * @UpdateRemark: The modified content
     * @Version: 1.0
     * @Return :        com.des.design.designMode.prototype.user
     **/

    public User getClone(User u) {
        User user = new User();
        user.setAge(u.getAge());
        user.setId(u.getId());
        user.setName(u.getName());
        return user;
    }

    //自定义泛型克隆方法
//    @Override
//    public Object getClone() {
//        user user = new user();
//        user.setAge(this.getAge());
//        user.setId(this.getId());
//        user.setName(this.getName());
//        return user;
//    }


    //通过java 自带的克隆类实现它重写克隆方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public static Object getJSONArrayClone(User u) {
        List <User> userList = new ArrayList <>();
        userList.add(u);
        return JSON.parseArray(JSON.toJSONString(userList), User.class);

    }

    public static Object getJSONArrayMapClone(User u) {
        Map <String, User> map = new HashMap <>();
        map.put("user", u);
        User user = JSON.toJavaObject((JSON) JSON.toJSON(map), User.class);
        return user;

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Long getIsmale() {
        return ismale;
    }

    public void setIsmale(Long ismale) {
        this.ismale = ismale;
    }
}
